package com.goodflower.goodflower;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.goodflower.goodflower.chang_mac.Change_myflowerItem_image;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FLowerInfo extends AppCompatActivity {
    private List<PointValue> valueskongshi;
    private List<PointValue> valueskongwen;
    private List<PointValue> valuesturang;
    private List<String> date_time;
    private View kongshiView;
    private View kongwenView;
    private View turangView;
    private FloatingActionButton btn_shezhi;
    private Calendar CD = Calendar.getInstance();
    private int YY = CD.get(Calendar.YEAR);
    private int MM = CD.get(Calendar.MONTH);
    private int DD = CD.get(Calendar.DATE);
    private int HH = CD.get(Calendar.HOUR);
    private int NN = CD.get(Calendar.MINUTE);
    private int SS = CD.get(Calendar.SECOND);
    private int MI = CD.get(Calendar.MILLISECOND);
    private int bug_goujian = 0;
    private LineChartView turangChart;
    private LineChartView kongshiChart;
    private LineChartView kongwenChart;
    private Line turangLine;
    private List<Line> turangLines;
    private Viewport turangV;
    private Viewport kongwenV;
    private Viewport kongshiV;
    private LineChartData turang_data;

    private Axis turang_axisX;


    private String flowername;
    private String username;
    private String mac;
    private int id;
    // private int id_baocun;


    private String[] at_split;
    private ProgressDialog jiazai;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Intent intent = getIntent();
        valueskongshi = new ArrayList<PointValue>();//这是图标里的数据
        valueskongwen = new ArrayList<PointValue>();
        valuesturang = new ArrayList<PointValue>();
        date_time = new ArrayList<String>();
        flowername = intent.getStringExtra("flowername");
        username = intent.getStringExtra("username");
        mac = intent.getStringExtra("mac");
        id = intent.getIntExtra("id", 0);
        // id_baocun = id;
        Log.d("chuansong", "中间界面>" + id);
        setContentView(R.layout.activity_flower_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbarLayout.setTitle(flowername);
        /***设置标题栏的图片**/
        ImageView toolbar_flower_image = (ImageView) findViewById(R.id.flower_image_view);
        Glide.with(getApplicationContext()).load("http://"+Info.server_ip+"/huahua/floweruser/" + username + "/" + mac + "/" + id + ".jpg").signature(new StringSignature(UUID.randomUUID().toString())).into(toolbar_flower_image);
        // Glide.with(getApplicationContext()).load("http://"+Info.server_ip+"/huahua/userpic/" + username + ".jpg").signature(new StringSignature(UUID.randomUUID().toString())) .into(icon);
        /*****/


        /**设置按钮**/
        btn_shezhi = (FloatingActionButton) findViewById(R.id.shezhi);
        btn_shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(FLowerInfo.this, v);
                popupMenu.getMenuInflater().inflate(R.menu.flowerinfo_shezhi_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // Toast.makeText(FLowerInfo.this,"你点了"+item.getTitle(),Toast.LENGTH_LONG).show();
                        if (item.getTitle().equals("更换查看时间")) {

                            time_select();
                        }
                        if (item.getTitle().equals("修改花卉参数")) {
                            chang_tishi();
                        }
                        if (item.getTitle().equals("修改花卉图片")) {
                            Intent take_flower_Item_photo_intent = new Intent(FLowerInfo.this, Change_myflowerItem_image.class);
                            take_flower_Item_photo_intent.putExtra("mac", mac);
                            take_flower_Item_photo_intent.putExtra("id", Integer.toString(id));
                            startActivity(take_flower_Item_photo_intent);
                        }
                        return false;
                    }
                });
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {
                        //Toast.makeText(MainActivity.this,"你关闭",Toast.LENGTH_LONG).show();
                    }
                });
                popupMenu.show();
            }
        });
        /*****/
        initvalues("2016", "12", "31", id, mac);

//        try {
//            Thread.sleep(2000);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        valueskongwen.clear();
        valueskongshi.clear();
        valuesturang.clear();
        date_time.clear();
        initvalues("2016", "12", "31", id, mac);
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

    }

    private void chang_tishi() {
        final SharedPreferences sharedPreferences = getSharedPreferences("date", MODE_PRIVATE);
        String tishi = sharedPreferences.getString("tishi", null);
        if (tishi.equals("0")) {
            //提示
            AlertDialog.Builder builder = new AlertDialog.Builder(FLowerInfo.this);
            final View view = View.inflate(FLowerInfo.this, R.layout.tishi, null);
            builder.setView(view);
            builder.setTitle("提示");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    CheckBox checkBox = (CheckBox) view.findViewById(R.id.tishi_chose);
                    if (checkBox.isChecked()) {
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("tishi", "1");
                        editor.apply();
                    }
                    chang_flower_shezhi();
                }
            });
            builder.show();

        } else {

            //不提示直接上
            chang_flower_shezhi();
        }

    }

    private void chang_flower_shezhi() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(FLowerInfo.this);
        final View view = View.inflate(FLowerInfo.this, R.layout.shezhi_flower, null);
        final EditText shezhi_kongshi = (EditText) view.findViewById(R.id.shezhi_kongshi);
        final EditText shezhi_kongwen = (EditText) view.findViewById(R.id.shezhi_kongwen);
        final EditText shezhi_tushi = (EditText) view.findViewById(R.id.shezhi_tushi);
        final EditText shezhi_jt = (EditText) view.findViewById(R.id.shezhi_jt);
        final EditText shezhi_pt = (EditText) view.findViewById(R.id.shezhi_pt);
        final RadioButton rdbtn_smart = (RadioButton) view.findViewById(R.id.shezhi_smart);
        final RadioButton rdbtn_putong = (RadioButton) view.findViewById(R.id.shezhi_putong);
        final EditText shezhi_atname = (EditText) view.findViewById(R.id.shezhi_atname);
        builder.setView(view);
        builder.setTitle("设置");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                at_split[id - 1] = String.valueOf(shezhi_tushi.getText());
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < at_split.length; i++) {
                    sb.append(at_split[i] + "|");
                }
                String chang_at = sb.toString().substring(0, sb.toString().length() - 1);

                String chang_kongwen = String.valueOf(shezhi_kongwen.getText());
                String chang_kongshi = String.valueOf(shezhi_kongshi.getText());
                String chang_jt = String.valueOf(shezhi_jt.getText());
                String chang_pt = String.valueOf(shezhi_pt.getText());
                String chang_mode = "0";
                String chang_atname = String.valueOf(shezhi_atname.getText());
                if (rdbtn_smart.isChecked()) {
                    chang_mode = "1";
                }
                HttpUtil.sendOkHttpRequest("http://"+Info.server_ip+"/huahua/3.php?mac=" + mac + "&kw=" + chang_kongwen + "&ks=" + chang_kongshi + "&mode=" + chang_mode + "&jt=" + chang_jt + "&pt=" + chang_pt + "&at=" + chang_at+"&atname="+chang_atname+"&id="+id, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                    }
                });
                Toast.makeText(FLowerInfo.this, "数据设置成功", Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpUtil.sendOkHttpRequest("http://"+ Info.server_ip+"/huahua/pgetpeizhi.php?mac=" + mac+"&id="+id, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res = response.body().string();
                        final String kongshi;
                        final String kongwen;
                        final String mode;
                        final String jt;
                        final String pt;
                        final String at;
                        final String atname;
                        try {
                            JSONObject jsonObject = new JSONObject(res);
                            mode = jsonObject.getString("mode");
                            kongshi = jsonObject.getString("kongshi");
                            kongwen = jsonObject.getString("kongwen");
                            jt = jsonObject.getString("jt");
                            at = jsonObject.getString("at");
                            pt = jsonObject.getString("pt");
                            atname = jsonObject.getString("atname");

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    shezhi_atname.setText(atname);
                                    shezhi_kongshi.setText(kongshi);
                                    shezhi_kongwen.setText(kongwen);
                                    shezhi_jt.setText(jt);
                                    shezhi_pt.setText(pt);
                                    at_split = at.split("\\|");
                                    shezhi_tushi.setText(at_split[id - 1]);
                                    if (mode.equals("1")) {
                                        rdbtn_smart.setChecked(true);
                                    } else {
                                        rdbtn_putong.setChecked(true);
                                    }
                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }).start();
    }

    private void goujianTurangbiao() {

        /*******构建表*********/
        kongwenView = findViewById(R.id.kongwen);
        kongshiView = findViewById(R.id.kongshi);

        turangView = findViewById(R.id.turang);
        LineChartView kongwenChart = (LineChartView) kongwenView.findViewById(R.id.line_chart);
        LineChartView kongshiChart = (LineChartView) kongshiView.findViewById(R.id.line_chart);

        turangChart = (LineChartView) turangView.findViewById(R.id.line_chart);


        turangLine = new Line(valuesturang).setColor(Color.BLUE).setCubic(true);
        turangLines = new ArrayList<Line>();
        turangLines.add(turangLine);
        turang_data = new LineChartData();
        //turangLine.setCubic(false);
        turangLine.setHasLabels(false);
        turangLine.setFilled(true);
        turangLine.setPointRadius(4);
        turangLine.setHasLabelsOnlyForSelected(true);
        turang_data.setLines(turangLines);
        turangChart.setBottom(3);
        turangChart.setLineChartData(turang_data);

        List<AxisValue> adate = new ArrayList<>();
        int timei = 0;
        for (String mytime : date_time) {
            // String[] split_time = mytime.split(":");
            // String real_time = split_time[3] + ":" + split_time[4];
            adate.add(new AxisValue(timei, mytime.toCharArray()));
            timei++;
        }

        turang_axisX = new Axis(); //X轴
        turang_axisX.setHasTiltedLabels(true);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
        turang_axisX.setTextColor(Color.BLUE);  //设置字体颜色
        turang_axisX.setName("时间");  //表格名称
        turang_axisX.setTextSize(12);//设置字体大小
        turang_axisX.setMaxLabelChars(8); //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
        turang_axisX.setValues(adate);  //填充X轴的坐标名称
        turang_data.setAxisXBottom(turang_axisX); //x 轴在底部
        //data.setAxisXTop(axisX);  //x 轴在顶部
        turang_axisX.setHasLines(true); //x 轴分割线

        Axis turangaxisY = new Axis();  //Y轴
        turangaxisY.setName("土壤湿度");//y轴标注
        turangaxisY.setTextSize(10);//设置字体大小
        turangaxisY.setTextColor(Color.BLUE);


        turang_data.setAxisYLeft(turangaxisY);  //Y轴设置在左边
        //data.setAxisYRight(axisY);  //y轴设置在右边


        turangV = new Viewport(turangChart.getMaximumViewport());
        turangV.left = 0;
        turangV.right = 7;
        turangChart.setCurrentViewport(turangV);
        /****************/

    }


    private void goujianKongwenbiao() {

        /*******构建表*********/
        kongwenView = findViewById(R.id.kongwen);
        kongshiView = findViewById(R.id.kongshi);

        turangView = findViewById(R.id.turang);
        kongwenChart = (LineChartView) kongwenView.findViewById(R.id.line_chart);


        Line kongwenLine = new Line(valueskongwen).setColor(Color.BLUE).setCubic(true);
        List<Line> kongwenLines = new ArrayList<Line>();
        kongwenLines.add(kongwenLine);
        LineChartData data = new LineChartData();
        kongwenLine.setCubic(true);
        kongwenLine.setHasLabels(false);
        kongwenLine.setFilled(true);
        kongwenLine.setPointRadius(4);
        kongwenLine.setHasLabelsOnlyForSelected(true);
        data.setLines(kongwenLines);
        kongwenChart.setBottom(3);
        kongwenChart.setLineChartData(data);

        List<AxisValue> adate = new ArrayList<>();
        int timei = 0;
        for (String mytime : date_time) {
            //String[] split_time = mytime.split(":");
            //String real_time = split_time[3] + ":" + split_time[4];
            adate.add(new AxisValue(timei, mytime.toCharArray()));
            timei++;
        }
        //  adate.add(new AxisValue());
        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(true);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
        axisX.setTextColor(Color.BLUE);  //设置字体颜色
        axisX.setName("时间");  //表格名称
        axisX.setTextSize(12);//设置字体大小
        axisX.setMaxLabelChars(8); //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
        axisX.setValues(adate);  //填充X轴的坐标名称
        data.setAxisXBottom(axisX); //x 轴在底部
        //data.setAxisXTop(axisX);  //x 轴在顶部
        axisX.setHasLines(true); //x 轴分割线
        Axis axisY = new Axis();  //Y轴
        axisY.setName("空气温度");//y轴标注
        axisY.setTextSize(10);//设置字体大小
        axisY.setTextColor(Color.BLUE);


        data.setAxisYLeft(axisY);  //Y轴设置在左边
        //data.setAxisYRight(axisY);  //y轴设置在右边


        kongwenV = new Viewport(kongwenChart.getMaximumViewport());
        kongwenV.left = 0;
        kongwenV.right = 7;
        kongwenChart.setCurrentViewport(kongwenV);
        /****************/

    }

    private void goujianKongshibiao() {
        /*******构建表*********/
        kongshiView = findViewById(R.id.kongshi);
        kongwenView = findViewById(R.id.kongwen);
        turangView = findViewById(R.id.turang);

        kongshiChart = (LineChartView) kongshiView.findViewById(R.id.line_chart);


        Line kongshiLine = new Line(valueskongshi).setColor(Color.BLUE).setCubic(true);
        List<Line> kongshiLines = new ArrayList<Line>();
        kongshiLines.add(kongshiLine);
        LineChartData data = new LineChartData();
        kongshiLine.setCubic(true);
        kongshiLine.setHasLabels(false);
        kongshiLine.setFilled(true);
        kongshiLine.setPointRadius(4);
        kongshiLine.setHasLabelsOnlyForSelected(true);
        // kongshiLine.setHasPoints(false);
        data.setLines(kongshiLines);
        kongshiChart.setBottom(3);
        kongshiChart.setLineChartData(data);

        List<AxisValue> adate = new ArrayList<>();
        int timei = 0;
        for (String mytime : date_time) {
            // String[] split_time = mytime.split(":");
            //String real_time = split_time[3] + ":" + split_time[4];
            adate.add(new AxisValue(timei, mytime.toCharArray()));
            timei++;
        }

        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(true);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
        axisX.setTextColor(Color.BLUE);  //设置字体颜色
        axisX.setName("时间");  //表格名称
        axisX.setTextSize(12);//设置字体大小
        axisX.setMaxLabelChars(8); //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
        axisX.setValues(adate);  //填充X轴的坐标名称
        data.setAxisXBottom(axisX); //x 轴在底部

        axisX.setHasLines(true); //x 轴分割线
        Axis axisY = new Axis();  //Y轴
        axisY.setName("空气湿度");//y轴标注
        axisY.setTextSize(11);//设置字体大小
        axisY.setTextColor(Color.BLUE);
        data.setAxisYLeft(axisY);  //Y轴设置在左边


        kongshiV = new Viewport(kongshiChart.getMaximumViewport());
        kongshiV.left = 0;
        kongshiV.right = 7;
        kongshiChart.setCurrentViewport(kongshiV);
        /****************/
    }

    private void initvalues(String YEAR, String MONTH, String DAY, int ID, String MAC) {

        if (bug_goujian == 0) {
            bug_goujian=1;
            for (int i = 0; i < 250; i++) {
                if (i == 0) {
                    valuesturang.add(new PointValue(i, 100));
                    valueskongshi.add(new PointValue(i, 100));
                    valueskongwen.add(new PointValue(i, -30));
                }
                if (i == 1) {
                    valuesturang.add(new PointValue(i, 0));
                    valueskongshi.add(new PointValue(i, 0));
                    valueskongwen.add(new PointValue(i, 50));
                }

                valuesturang.add(new PointValue(i, 0));
                valueskongshi.add(new PointValue(i, 0));
                valueskongwen.add(new PointValue(i, 0));
            }
            /****/




            for (int shi = 0; shi <= 24; shi++) {
                for (int fen = 0; fen < 6; fen++) {
                    if (fen == 0) {
                        date_time.add(shi + "点");
                    } else if (fen == 3) {
                        date_time.add(shi + "半");
                    } else {
                        date_time.add(" ");
                    }
                }
            }
            goujianTurangbiao();
            goujianKongshibiao();
            goujianKongwenbiao();


            /****/

        }
        else {


            HttpUtil.sendOkHttpRequest("http://" + Info.server_ip + "/huahua/shuju.php?y=" + YEAR + "&m=" + MONTH + "&d=" + DAY + "&id=" + ID + "&mac=" + MAC, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String rep = response.body().string();
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(rep);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    /*******所用开源框架的bug tmd ***/
                    int kongwenflag = 0;
                    int kongshiflag = 0;
                    int turangflag = 0;
                    int chushikongwen = 0;
                    int chushikongshi = 0;
                    int chushiturang = 0;


                    /****/
                    for (int i = 0; i < jsonArray.length(); i++) {

                        try {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String kongshi = jsonObject.getString("kongshi");
                            // valueskongshi.add(new PointValue(i, Integer.parseInt(kongshi)));

                            String kongwen = jsonObject.getString("kongwen");
                            // valueskongwen.add(new PointValue(i, Integer.parseInt(kongwen)));

                            String tushi = jsonObject.getString("d");
                            // valuesturang.add(new PointValue(i, Integer.parseInt(tushi)));

                            // String time = jsonObject.getString("time");
                            // date_time.add(time);
                            if (i == 0) {
                                chushikongshi = Integer.parseInt(kongshi);
                                chushikongwen = Integer.parseInt(kongwen);
                                chushiturang = Integer.parseInt(tushi);
                            } else {
                                if (chushikongshi != Integer.parseInt(kongshi)) kongshiflag = 1;
                                if (chushikongwen != Integer.parseInt(kongwen)) kongwenflag = 1;
                                if (chushiturang != Integer.parseInt(tushi)) turangflag = 1;
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                    /****/


//
//                if (kongshiflag == 0) valueskongshi.add(new PointValue(-1, -1));
//                if (kongwenflag == 0) valueskongwen.add(new PointValue(-1, -1));
//                if (turangflag == 0) valuesturang.add(new PointValue(-1, -1));
                    /********正式赋值***********/
                    for (int i = 0; i < jsonArray.length(); i++) {

                        try {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);

                            String time = jsonObject.getString("time");
                            String[] time_split = time.split(":");
                            int shiwei = Integer.parseInt(time_split[4]) / 10;
                            int gewei = Integer.parseInt(time_split[4]) - shiwei * 10;
                            int shiji_gewei = 0;
                            if (gewei >= 5) {
                                shiji_gewei = shiwei + 1;
                            } else {
                                shiji_gewei = shiwei;
                            }
                            int postion = Integer.parseInt(time_split[3]) * 6 + shiji_gewei;

                            String kongshi = jsonObject.getString("kongshi");
                            valueskongshi.add(new PointValue(postion, Integer.parseInt(kongshi)));

                            String kongwen = jsonObject.getString("kongwen");
                            valueskongwen.add(new PointValue(postion, Integer.parseInt(kongwen)));

                            String tushi = jsonObject.getString("d");
                            valuesturang.add(new PointValue(postion, Integer.parseInt(tushi)));


                            // date_time.add(time);


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    for (int shi = 0; shi <= 24; shi++) {
                        for (int fen = 0; fen < 6; fen++) {
                            if (fen == 0) {
                                date_time.add(shi + "点");
                            } else if (fen == 3) {
                                date_time.add(shi + "半");
                            } else {
                                date_time.add(" ");
                            }
                        }
                    }


                    kongwenChart.setCurrentViewport(kongwenV);
                    kongshiChart.setCurrentViewport(kongshiV);
                    turangChart.setCurrentViewport(turangV);

                }
            });
        }

    }

    private void time_select() {
        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // Log.d("time","你选择了"+year+(month+1)+""+dayOfMonth);
                YY = year;
                MM = month + 1;
                DD = dayOfMonth;

                valuesturang.clear();
                valueskongshi.clear();
                valueskongwen.clear();

                initvalues(YY + "", MM + "", DD + "", id, mac);


                try {

                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                /*****/


                // turang_data = new LineChartData();


                //turang_data.setLines(turangLines);

                List<AxisValue> adate = new ArrayList<>();
                int timei = 0;
                for (String mytime : date_time) {
                    // String[] split_time = mytime.split(":");
                    // String real_time = split_time[3] + ":" + split_time[4];
                    adate.add(new AxisValue(timei, mytime.toCharArray()));
                    timei++;
                }


                turang_axisX.setHasTiltedLabels(true);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
                turang_axisX.setTextColor(Color.BLACK);  //设置字体颜色
                //axisX.setName("date");  //表格名称
                turang_axisX.setTextSize(12);//设置字体大小
                turang_axisX.setMaxLabelChars(8); //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
                turang_axisX.setValues(adate);  //填充X轴的坐标名称
                turang_data.setAxisXBottom(turang_axisX); //x 轴在底部
                //data.setAxisXTop(axisX);  //x 轴在顶部
                turang_axisX.setHasLines(true); //x 轴分割线

                /******/
//
                turangV = new Viewport(turangChart.getMaximumViewport());
                turangV.left = 0;
                turangV.right = 7;
                turangChart.setCurrentViewport(turangV);

                kongshiV = new Viewport(kongshiChart.getMaximumViewport());
                kongshiV.left = 0;
                kongshiV.right = 7;
                kongshiChart.setCurrentViewport(kongshiV);

                kongwenV = new Viewport(kongwenChart.getMaximumViewport());
                kongwenV.left = 0;
                kongwenV.right = 7;
                kongwenChart.setCurrentViewport(kongwenV);

            }
        };


        DatePickerDialog datePickerDialog = new DatePickerDialog(FLowerInfo.this, dateSetListener, YY, MM, DD);
        datePickerDialog.show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

                return true;

            default:
        }
        return super.onOptionsItemSelected(item);
    }
}
